home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet internetowy / Przegladarki internetowe / Mozilla Seamonkey 1.0.5 pl / seamonkey-1.0.5.pl-PL.win32.installer.exe / MAIL.XPI / bin / chrome / messenger.jar / content / messenger / am-smime.js < prev    next >
Encoding:
Text File  |  2005-09-15  |  12.7 KB  |  412 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org Code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998-2001
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   ddrinan@netscape.com
  24.  *   Scott MacGregor <mscott@netscape.com>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  28.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. const nsIX509CertDB = Components.interfaces.nsIX509CertDB;
  41. const nsX509CertDBContractID = "@mozilla.org/security/x509certdb;1";
  42. const nsIX509Cert = Components.interfaces.nsIX509Cert;
  43.  
  44. const email_recipient_cert_usage = 5;
  45. const email_signing_cert_usage = 4;
  46.  
  47. var gIdentity;
  48. var gPref = null;
  49. var gEncryptionCertName = null;
  50. var gHiddenEncryptionPolicy = null;
  51. var gEncryptionChoices = null;
  52. var gSignCertName  = null;
  53. var gSignMessages  = null;
  54. var gEncryptAlways = null;
  55. var gNeverEncrypt = null;
  56. var gBundle = null;
  57. var gBrandBundle;
  58. var gSmimePrefbranch;
  59. var gEncryptionChoicesLocked;
  60. var gSigningChoicesLocked;
  61. const kEncryptionCertPref = "identity.encryption_cert_name";
  62. const kSigningCertPref = "identity.signing_cert_name";
  63.  
  64. function onInit() 
  65. {
  66.   // initialize all of our elements based on the current identity values....
  67.   gEncryptionCertName = document.getElementById(kEncryptionCertPref);
  68.   gHiddenEncryptionPolicy = document.getElementById("identity.encryptionpolicy");
  69.   gEncryptionChoices = document.getElementById("encryptionChoices");
  70.   gSignCertName       = document.getElementById(kSigningCertPref);
  71.   gSignMessages       = document.getElementById("identity.sign_mail");
  72.   gEncryptAlways      = document.getElementById("encrypt_mail_always");
  73.   gNeverEncrypt       = document.getElementById("encrypt_mail_never");
  74.   gBundle             = document.getElementById("bundle_smime");
  75.   gBrandBundle        = document.getElementById("bundle_brand");
  76.  
  77.   gEncryptionChoicesLocked = false;
  78.   gSigningChoicesLocked = false;
  79.  
  80.   gEncryptionCertName.value = gIdentity.getUnicharAttribute("encryption_cert_name");
  81.  
  82.   gEncryptionChoices.value = gIdentity.getIntAttribute("encryptionpolicy");
  83.     
  84.   if (!gEncryptionCertName.value)
  85.   {
  86.     gEncryptAlways.setAttribute("disabled", true);
  87.     gNeverEncrypt.setAttribute("disabled", true);
  88.   }
  89.   else {
  90.     enableEncryptionControls(true);
  91.   }
  92.  
  93.   gSignCertName.value = gIdentity.getUnicharAttribute("signing_cert_name");
  94.   gSignMessages.checked = gIdentity.getBoolAttribute("sign_mail");
  95.   if (!gSignCertName.value)
  96.   {
  97.     gSignMessages.setAttribute("disabled", true);
  98.   }
  99.   else {
  100.     enableSigningControls(true);
  101.   }
  102.  
  103.   // Always start with enabling signing and encryption cert select buttons.
  104.   // This will keep the visibility of buttons in a sane state as user
  105.   // jumps from security panel of one account to another.
  106.   enableCertSelectButtons();
  107.  
  108.   // Disable all locked elements on the panel
  109.   onLockPreference();
  110. }
  111.  
  112. function onPreInit(account, accountValues)
  113. {
  114.   gIdentity = account.defaultIdentity;
  115. }
  116.  
  117. function onSave()
  118. {
  119.   // find out which radio for the encryption radio group is selected and set that on our hidden encryptionChoice pref....
  120.   var newValue = gEncryptionChoices.value;
  121.   gHiddenEncryptionPolicy.setAttribute('value', newValue);
  122.   gIdentity.setIntAttribute("encryptionpolicy", newValue);
  123.   gIdentity.setUnicharAttribute("encryption_cert_name", gEncryptionCertName.value);
  124.  
  125.   gIdentity.setBoolAttribute("sign_mail", gSignMessages.checked);
  126.   gIdentity.setUnicharAttribute("signing_cert_name", gSignCertName.value);
  127. }
  128.  
  129. function onLockPreference()
  130. {
  131.   var initPrefString = "mail.identity"; 
  132.   var finalPrefString; 
  133.  
  134.   var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  135.  
  136.   var allPrefElements = [
  137.     { prefstring:"signingCertSelectButton", id:"signingCertSelectButton"},
  138.     { prefstring:"encryptionCertSelectButton", id:"encryptionCertSelectButton"},
  139.     { prefstring:"sign_mail", id:"identity.sign_mail"},
  140.     { prefstring:"encryptionpolicy", id:"encryptionChoices"}
  141.   ];
  142.  
  143.   finalPrefString = initPrefString + "." + gIdentity.key + ".";
  144.   gSmimePrefbranch = prefService.getBranch(finalPrefString);
  145.  
  146.   disableIfLocked( allPrefElements );
  147. }
  148.  
  149.  
  150. // Does the work of disabling an element given the array which contains xul id/prefstring pairs.
  151. // Also saves the id/locked state in an array so that other areas of the code can avoid
  152. // stomping on the disabled state indiscriminately.
  153. function disableIfLocked( prefstrArray )
  154. {
  155.   var i;
  156.   for (i=0; i<prefstrArray.length; i++) {
  157.     var id = prefstrArray[i].id;
  158.     var element = document.getElementById(id);
  159.     if (gSmimePrefbranch.prefIsLocked(prefstrArray[i].prefstring)) {
  160.       // If encryption choices radio group is locked, make sure the individual 
  161.       // choices in the group are locked. Set a global (gEncryptionChoicesLocked) 
  162.       // indicating the status so that locking can be maintained further.
  163.       if (id == "encryptionChoices") {
  164.         document.getElementById("encrypt_mail_never").setAttribute("disabled", "true");
  165.         document.getElementById("encrypt_mail_always").setAttribute("disabled", "true");
  166.         gEncryptionChoicesLocked = true;
  167.       }
  168.       // If option to sign mail is locked (with true/false set in config file), disable
  169.       // the corresponding checkbox and set a global (gSigningChoicesLocked) in order to
  170.       // honor the locking as user changes other elements on the panel. 
  171.       if (id == "identity.sign_mail") {
  172.         document.getElementById("identity.sign_mail").setAttribute("disabled", "true");
  173.         gSigningChoicesLocked = true;
  174.       }
  175.       else {
  176.         element.setAttribute("disabled", "true");
  177.         if (id == "signingCertSelectButton") {
  178.           document.getElementById("signingCertClearButton").setAttribute("disabled", "true");
  179.         }
  180.         else if (id == "encryptionCertSelectButton") {
  181.           document.getElementById("encryptionCertClearButton").setAttribute("disabled", "true");
  182.         }
  183.       }
  184.     }
  185.   }
  186. }
  187.  
  188. function getPromptService()
  189. {
  190.   var ifps = Components.interfaces.nsIPromptService;
  191.   var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
  192.   if (promptService) {
  193.     promptService = promptService.QueryInterface(ifps);
  194.   }
  195.   return promptService;
  196. }
  197.  
  198. function alertUser(message)
  199. {
  200.   var ps = getPromptService();
  201.   if (ps) {
  202.     ps.alert(
  203.       window,
  204.       gBrandBundle.getString("brandShortName"), 
  205.       message);
  206.   }
  207. }
  208.  
  209. function askUser(message)
  210. {
  211.   var ps = getPromptService();
  212.   if (!ps)
  213.     return false;
  214.  
  215.   var button = ps.confirmEx(
  216.     window,
  217.     gBrandBundle.getString("brandShortName"), 
  218.     message,
  219.     ps.STD_YES_NO_BUTTONS,
  220.     null,
  221.     null,
  222.     null,
  223.     null,
  224.     {});
  225.   // confirmEx returns button index:
  226.   return (button == 0);
  227. }
  228.  
  229. function checkOtherCert(nickname, pref, usage, msgNeedCertWantSame, msgWantSame, msgNeedCertWantToSelect, enabler)
  230. {
  231.   var otherCertInfo = document.getElementById(pref);
  232.   if (!otherCertInfo)
  233.     return;
  234.  
  235.   if (otherCertInfo.value == nickname)
  236.     // all is fine, same cert is now selected for both purposes
  237.     return;
  238.  
  239.   var certdb = Components.classes[nsX509CertDBContractID].getService(nsIX509CertDB);
  240.   if (!certdb)
  241.     return;
  242.   
  243.   if (email_recipient_cert_usage == usage) {
  244.     matchingOtherCert = certdb.findEmailEncryptionCert(nickname);
  245.   }
  246.   else if (email_signing_cert_usage == usage) {
  247.     matchingOtherCert = certdb.findEmailSigningCert(nickname);
  248.   }
  249.   else
  250.     return;
  251.  
  252.   var userWantsSameCert = false;
  253.  
  254.   if (!otherCertInfo.value.length) {
  255.     if (matchingOtherCert) {
  256.       userWantsSameCert = askUser(gBundle.getString(msgNeedCertWantSame));
  257.     }
  258.     else {
  259.       if (askUser(gBundle.getString(msgNeedCertWantToSelect))) {
  260.         smimeSelectCert(pref);
  261.       }
  262.     }
  263.   }
  264.   else {
  265.     if (matchingOtherCert) {
  266.       userWantsSameCert = askUser(gBundle.getString(msgWantSame));
  267.     }
  268.   }
  269.  
  270.   if (userWantsSameCert) {
  271.     otherCertInfo.value = nickname;
  272.     enabler(true);
  273.   }
  274. }
  275.  
  276. function smimeSelectCert(smime_cert)
  277. {
  278.   var certInfo = document.getElementById(smime_cert);
  279.   if (!certInfo)
  280.     return;
  281.  
  282.   var picker = Components.classes["@mozilla.org/user_cert_picker;1"]
  283.                .createInstance(Components.interfaces.nsIUserCertPicker);
  284.   var canceled = new Object;
  285.   var x509cert = 0;
  286.   var certUsage;
  287.   var selectEncryptionCert;
  288.  
  289.   if (smime_cert == kEncryptionCertPref) {
  290.     selectEncryptionCert = true;
  291.     certUsage = email_recipient_cert_usage;
  292.   } else if (smime_cert == kSigningCertPref) {
  293.     selectEncryptionCert = false;
  294.     certUsage = email_signing_cert_usage;
  295.   }
  296.  
  297.   try {
  298.     x509cert = picker.pickByUsage(window,
  299.       certInfo.value,
  300.       certUsage, // this is from enum SECCertUsage
  301.       false, false, canceled);
  302.   } catch(e) {
  303.     canceled.value = false;
  304.     x509cert = null;
  305.   }
  306.  
  307.   if (!canceled.value) {
  308.     if (!x509cert) {
  309.       var errorString;
  310.       if (selectEncryptionCert) {
  311.         errorString = "NoEncryptionCert";
  312.       }
  313.       else {
  314.         errorString = "NoSigningCert";
  315.       }
  316.       alertUser(gBundle.getString(errorString));
  317.     }
  318.     else {
  319.       certInfo.removeAttribute("disabled");
  320.       certInfo.value = x509cert.nickname;
  321.  
  322.       if (selectEncryptionCert) {
  323.         enableEncryptionControls(true);
  324.  
  325.         checkOtherCert(certInfo.value,
  326.           kSigningCertPref, email_signing_cert_usage, 
  327.           "signing_needCertWantSame", 
  328.           "signing_wantSame", 
  329.           "signing_needCertWantToSelect",
  330.           enableSigningControls);
  331.       } else {
  332.         enableSigningControls(true);
  333.  
  334.         checkOtherCert(certInfo.value,
  335.           kEncryptionCertPref, email_recipient_cert_usage, 
  336.           "encryption_needCertWantSame", 
  337.           "encryption_wantSame", 
  338.           "encryption_needCertWantToSelect",
  339.           enableEncryptionControls);
  340.       }
  341.     }
  342.   }
  343.  
  344.   enableCertSelectButtons();
  345. }
  346.  
  347. function enableEncryptionControls(do_enable)
  348. {
  349.   if (gEncryptionChoicesLocked)
  350.     return;
  351.  
  352.   if (do_enable) {
  353.     gEncryptAlways.removeAttribute("disabled");
  354.     gNeverEncrypt.removeAttribute("disabled");
  355.   }
  356.   else {
  357.     gEncryptAlways.setAttribute("disabled", "true");
  358.     gNeverEncrypt.setAttribute("disabled", "true");
  359.     gEncryptionChoices.value = 0;
  360.   }
  361. }
  362.  
  363. function enableSigningControls(do_enable)
  364. {
  365.   if (gSigningChoicesLocked)
  366.     return;
  367.  
  368.   if (do_enable) {
  369.     gSignMessages.removeAttribute("disabled");
  370.   }
  371.   else {
  372.     gSignMessages.setAttribute("disabled", "true");
  373.     gSignMessages.checked = false;
  374.   }
  375. }
  376.  
  377. function enableCertSelectButtons()
  378. {
  379.   document.getElementById("signingCertSelectButton").removeAttribute("disabled");
  380.  
  381.   if (document.getElementById('identity.signing_cert_name').value.length)
  382.     document.getElementById("signingCertClearButton").removeAttribute("disabled");
  383.   else
  384.     document.getElementById("signingCertClearButton").setAttribute("disabled", "true");
  385.  
  386.   document.getElementById("encryptionCertSelectButton").removeAttribute("disabled");
  387.  
  388.   if (document.getElementById('identity.encryption_cert_name').value.length)
  389.     document.getElementById("encryptionCertClearButton").removeAttribute("disabled");
  390.   else
  391.     document.getElementById("encryptionCertClearButton").setAttribute("disabled", "true");
  392. }
  393.  
  394. function smimeClearCert(smime_cert)
  395. {
  396.   var certInfo = document.getElementById(smime_cert);
  397.   if (!certInfo)
  398.     return;
  399.  
  400.   certInfo.setAttribute("disabled", "true");
  401.   certInfo.value = "";
  402.  
  403.   if (smime_cert == kEncryptionCertPref) {
  404.     enableEncryptionControls(false);
  405.   } else if (smime_cert == kSigningCertPref) {
  406.     enableSigningControls(false);
  407.   }
  408.   
  409.   enableCertSelectButtons();
  410. }
  411.  
  412.